package com.example.demo.service.impl;

import com.example.demo.service.WeatherService;
import com.example.demo.untils.HtmlUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
public class WeatherServiceImpl implements WeatherService {
    @Override
    public List<Map<String, String>> getCityWeatherDataList(String cityCode) {
        System.out.println(cityCode);
        String result=HtmlUtil.getResult("http://www.weather.com.cn/weather/"+cityCode+".shtml");
        Document document= Jsoup.parse(result);
        Elements elements;

        // 获取日期和星期
        elements=document.select("h1");
        List<String> dateList=new ArrayList<>();
        List<String> dayList=new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            String text=elements.get(i).text();
            int length=text.length();
            dateList.add(text.substring(0,length-4));
            dayList.add(text.substring(length-3,length-1));
        }
        System.out.println(dateList);
        System.out.println(dayList);

        // 获取天气
        elements=document.select("p[class=wea]");
        List<String> weatherList=new ArrayList<>();
        for (Element item : elements) {
            weatherList.add(item.text());
        }
        System.out.println(weatherList);

        // 获取温度，最高温和最低温
        elements=document.select("p[class=tem]");
        int i=0;
        List<String> highTempList=new ArrayList<>();
        List<String> lowTempList=new ArrayList<>();
        for (Element item : elements) {
            highTempList.add(item.select("span").text()+"℃");
            lowTempList.add(item.select("i").text());
        }
        System.out.println(highTempList);
        System.out.println(lowTempList);

        // 获取风向及风力
        elements=document.select("p[class=win]");
        List<String> windDirectionList1=new ArrayList<>();
        List<String> windDirectionList2=new ArrayList<>();
        List<String> windSpeedList=new ArrayList<>();
        for (Element item : elements) {
            Element em=item.child(0); //获取em标签，em标签中包含了两个span标签，是需要的风向
            if(em.select("span").size() == 1){
                windDirectionList1.add(em.select("span").get(0).attr("title")); //attr函数用来获取标签内的属性值
            }else if(em.select("span").size() == 2){
                windDirectionList1.add(em.select("span").get(0).attr("title")); //attr函数用来获取标签内的属性值
                windDirectionList2.add(em.select("span").get(1).attr("title"));
            }
            windSpeedList.add(item.select("i").text());
        }
        System.out.println(windDirectionList1);
        System.out.println(windDirectionList2);
        System.out.println(windSpeedList);

        // 封装结果，每天一行
        List<Map<String,String>> list=new ArrayList<>();
        Map<String,String> map = null;
        for (int j = 0; j < 7; j++) {
            map=new LinkedHashMap<>();
            map.put("date",dateList.get(j));
            map.put("day",dayList.get(j));
            map.put("weather",weatherList.get(j));
            map.put("highTemp",highTempList.get(j));
            map.put("lowTemp",lowTempList.get(j));
            map.put("windDirection1",windDirectionList1.get(j));
            map.put("windSpeed",windSpeedList.get(j));
            list.add(map);
        }
        for(int k = 0;k < windDirectionList2.size();k++ ){
            map.put("windDirection2",windDirectionList2.get(k));
        }
        return list;
    }
}
